//
//  CustomTableViewController.m
//  TopStock
//
//  Created by dyd on 2017/11/6.
//  Copyright © 2017年 dyd. All rights reserved.
//

#import "CustomTableViewController.h"
#import "CustomCell.h"
#import "CustomView.h"

@interface CustomTableViewController ()
{
    NSMutableArray *dataArray;
}

@end

@implementation CustomTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCalendarInfoMation:) name:@"getCalendarInfoMation" object:nil];
    dataArray = [[NSMutableArray alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"customCell"];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self.tableView reloadData];
    self.tableView.separatorInset = UIEdgeInsetsZero;
}


- (void)getCalendarInfoMation:(NSNotification*)notification
{
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    if (dataArray != nil){
        [dataArray removeAllObjects];
    }
    if ([notification.object[@"data"] isKindOfClass:[NSNull class]])
    {
        
    }else{
        dataArray = [[[NSMutableArray alloc] initWithArray:notification.object[@"data"]] mutableCopy];
    }
    
    if ([dataArray count] == 0){
        CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] lastObject];
        self.tableView.tableFooterView = customView;
        self.tableView.scrollEnabled = NO;
        
    }else{
        self.tableView.scrollEnabled = YES;
        self.tableView.tableFooterView = nil;
    }
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSString *product_name = [self checkString:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"product_name"]];//产品名称
    
    NSInteger target_status = [[[dataArray objectAtIndex:indexPath.row] objectForKey:@"target_status"] integerValue];//预定状态
    NSString *pay_date = [self checkString:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"pay_date"]];//订单时间
    if (![pay_date isEqual:@""]){
        pay_date = [self timeWithTimeIntervalString:pay_date];//时间戳转化为时间
    }
    NSString *investment_time = [self checkString:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"investment_time"]];//产品期限
    NSString *user_name = [self checkString:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"user_name"]];//客服名称
    NSString *pay_amount = [self checkString:[[dataArray objectAtIndex:indexPath.row] objectForKey:@"pay_amount"]];//产品价格
    
//    NSLog(@"investment_time ==>:%@",investment_time);
//    NSLog(@"pay_amount ==>:%@",pay_amount);
//    NSLog(@"pay_date ==>:%@",pay_date);
//    NSLog(@"product_name ==>:%@",product_name);
//    NSLog(@"user_name ==>:%@",user_name);

    cell.product_name.text = product_name;
    cell.pay_date.text = [NSString stringWithFormat:@"预约时间:%@",pay_date];
    cell.investment_time.text = [NSString stringWithFormat:@"产品期限:%@",investment_time];
    cell.user_name.text = [NSString stringWithFormat:@"客服名称:%@",user_name];
    cell.pay_amount.text = [NSString stringWithFormat:@"%@万",pay_amount];
    
    
    switch (target_status) {
        case 0:
            cell.target_status.text= @"待审核";
            break;
        case 1:
            cell.target_status.text= @"审核失败";
            break;
        case 2:
            cell.target_status.text= @"预约中";
            break;
        case 3:
            cell.target_status.text= @"预约失败";
            break;
        case 4:
            cell.target_status.text= @"待报单";
            break;
        case 5:
            cell.target_status.text= @"报单失败";
            break;
        case 6:
            cell.target_status.text= @"待结佣";
            break;
        case 7:
            cell.target_status.text= @"结佣成功";
            break;
        case 8:
            cell.target_status.text= @"结佣失败";
            break;
        default:
            cell.target_status.text= @"未知";
            break;
    }
    return cell;
}

- (NSString*)checkString:(NSString *)str
{
    if ([str isKindOfClass:[NSNull class]])
    {
        return @"";
    }else{
        return str;
    }
}

- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
